package org.hiro.actions;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import org.hiro.psi.PsiHelper;

public class GenerateWithMethodAction extends EditorAction {

    protected GenerateWithMethodAction() {
        super(new GenerateWithMethodActionHandler());
    }

    public void update(Editor editor, Presentation presentation, DataContext dataContext) {
        PsiHelper psiHelper = new PsiHelper(dataContext);
        PsiFile selectedFile = psiHelper.getCurrentFile();

        boolean enable = false;

        if (selectedFile instanceof PsiJavaFile) {
            PsiClass psiClass = psiHelper.getCurrentClass();
            enable = isJavaFileAndNotAnInterface(selectedFile, psiClass);
        }
        presentation.setEnabled(enable);
    }

    private boolean isJavaFileAndNotAnInterface(PsiFile selectedFile, PsiClass psiClass) {
        return (selectedFile instanceof PsiJavaFile) && (psiClass != null) && !psiClass.isInterface();
    }
}
